
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5.模拟实现call和apply · Gary的小窝</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gary">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ui.html" />
    
    
    <link rel="prev" href="proto.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    写在前面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../leetcode/">
            
                <a href="../leetcode/">
            
                    
                    Leetcode之旅
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../leetcode/1.html">
            
                <a href="../leetcode/1.html">
            
                    
                    1.两数之和
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../leetcode/2.html">
            
                <a href="../leetcode/2.html">
            
                    
                    2.两数相加
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../leetcode/3.html">
            
                <a href="../leetcode/3.html">
            
                    
                    3.无重复字符的最长子串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../leetcode/4.html">
            
                <a href="../leetcode/4.html">
            
                    
                    4.寻找两个有序数组的中位数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../leetcode/6.html">
            
                <a href="../leetcode/6.html">
            
                    
                    6.Z字形变换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../leetcode/9.html">
            
                <a href="../leetcode/9.html">
            
                    
                    9.回文数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../leetcode/11.html">
            
                <a href="../leetcode/11.html">
            
                    
                    11.盛最多水的容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../leetcode/12.html">
            
                <a href="../leetcode/12.html">
            
                    
                    12.整数转罗马数字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../leetcode/13.html">
            
                <a href="../leetcode/13.html">
            
                    
                    13.罗马数字转整数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../leetcode/14.html">
            
                <a href="../leetcode/14.html">
            
                    
                    14.最长公共前缀
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../leetcode/322.html">
            
                <a href="../leetcode/322.html">
            
                    
                    322.零钱兑换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../leetcode/72.html">
            
                <a href="../leetcode/72.html">
            
                    
                    72.编辑距离
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../flutter/">
            
                <a href="../flutter/">
            
                    
                    Flutter从入门到放弃
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../flutter/dart1.html">
            
                <a href="../flutter/dart1.html">
            
                    
                    1.Dart语言(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../flutter/dart2.html">
            
                <a href="../flutter/dart2.html">
            
                    
                    2.Dart语言(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../flutter/widget1.html">
            
                <a href="../flutter/widget1.html">
            
                    
                    3.Flutter初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../flutter/containerText.html">
            
                <a href="../flutter/containerText.html">
            
                    
                    4.Container和Text组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../flutter/image.html">
            
                <a href="../flutter/image.html">
            
                    
                    5.Image组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../flutter/chapter2.html">
            
                <a href="../flutter/chapter2.html">
            
                    
                    6.Flutter 技术总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    Vue组件精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../vue/1.html">
            
                <a href="../vue/1.html">
            
                    
                    1.provide/inject
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../vue/2.html">
            
                <a href="../vue/2.html">
            
                    
                    2.手动实现broadcast和dispatch方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="form.html">
            
                <a href="form.html">
            
                    
                    3.自己动手实现带校验的Form表单组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    JavaScript进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="proxy.html">
            
                <a href="proxy.html">
            
                    
                    1.Vue.js 3.0中的响应式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="el.html">
            
                <a href="el.html">
            
                    
                    2.Element UI 源码初探
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="bind.html">
            
                <a href="bind.html">
            
                    
                    3.bind的模拟实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="proto.html">
            
                <a href="proto.html">
            
                    
                    4.JS原型和原型链
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.5" data-path="call.html">
            
                <a href="call.html">
            
                    
                    5.模拟实现call和apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="ui.html">
            
                <a href="ui.html">
            
                    
                    6.使用Vue cli3搭建组件库并发布到 npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="microFE.html">
            
                <a href="microFE.html">
            
                    
                    7.记一次基于qiankun的微前端的改造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="git.html">
            
                <a href="git.html">
            
                    
                    8.Git的使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vueLibrary/">
            
                <a href="../vueLibrary/">
            
                    
                    基于Vue的PC端的组件库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vueLibrary/1.html">
            
                <a href="../vueLibrary/1.html">
            
                    
                    1.环境搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../life/">
            
                <a href="../life/">
            
                    
                    网事杂谈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../life/1.html">
            
                <a href="../life/1.html">
            
                    
                    1.周杰伦专辑赏析--叶惠美
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../life/2.html">
            
                <a href="../life/2.html">
            
                    
                    2.周杰伦专辑赏析--七里香
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >5.模拟实现call和apply</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#javascript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>JavaScript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li><ul><li><span class="title-icon "></span><a href="#call"><b></b>call</a></li><li><span class="title-icon "></span><a href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;"><b></b>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;</a></li><li><span class="title-icon "></span><a href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;"><b></b>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;</a></li><li><span class="title-icon "></span><a href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;"><b></b>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;</a></li><li><span class="title-icon "></span><a href="#apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li></ul></ul></div><a href="#javascript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="javascript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="javascript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#javascript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>JavaScript&#x6DF1;&#x5165;&#x4E4B;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h1>
<blockquote>
<p>&#x901A;&#x8FC7;call&#x548C;apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#xFF0C;&#x5E26;&#x4F60;&#x63ED;&#x5F00;call&#x548C;apply&#x6539;&#x53D8;this&#x7684;&#x771F;&#x76F8;</p>
</blockquote>
<h2 id="call"><a name="call" class="anchor-navigation-ex-anchor" href="#call"><i class="fa fa-link" aria-hidden="true"></i></a>call</h2>
<p>&#x4E00;&#x53E5;&#x8BDD;&#x4ECB;&#x7ECD; call&#xFF1A;</p>
<blockquote>
<p>call() &#x65B9;&#x6CD5;&#x5728;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7684; this &#x503C;&#x548C;&#x82E5;&#x5E72;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x53C2;&#x6570;&#x503C;&#x7684;&#x524D;&#x63D0;&#x4E0B;&#x8C03;&#x7528;&#x67D0;&#x4E2A;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#x4E24;&#x70B9;&#xFF1A;</p>
<ol>
<li>call &#x6539;&#x53D8;&#x4E86; this &#x7684;&#x6307;&#x5411;&#xFF0C;&#x6307;&#x5411;&#x5230; foo</li>
<li>bar &#x51FD;&#x6570;&#x6267;&#x884C;&#x4E86;</li>
</ol>
<h2 id="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;"><a name="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;" class="anchor-navigation-ex-anchor" href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E00;&#x6B65;</h2>
<p>&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x8BE5;&#x600E;&#x4E48;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x8FD9;&#x4E24;&#x4E2A;&#x6548;&#x679C;&#x5462;&#xFF1F;</p>
<p>&#x8BD5;&#x60F3;&#x5F53;&#x8C03;&#x7528; call &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x628A; foo &#x5BF9;&#x8C61;&#x6539;&#x9020;&#x6210;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token function-variable function">bar</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

foo<span class="token punctuation">.</span><span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65F6;&#x5019; this &#x5C31;&#x6307;&#x5411;&#x4E86; foo&#xFF0C;&#x662F;&#x4E0D;&#x662F;&#x5F88;&#x7B80;&#x5355;&#x5462;&#xFF1F;</p>
<p>&#x4F46;&#x662F;&#x8FD9;&#x6837;&#x5374;&#x7ED9; foo &#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x53EF;&#x4E0D;&#x884C;&#x5450;&#xFF01;</p>
<p>&#x4E0D;&#x8FC7;&#x4E5F;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#xFF0C;&#x6211;&#x4EEC;&#x7528; delete &#x518D;&#x5220;&#x9664;&#x5B83;&#x4E0D;&#x5C31;&#x597D;&#x4E86;~</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x6A21;&#x62DF;&#x7684;&#x6B65;&#x9AA4;&#x53EF;&#x4EE5;&#x5206;&#x4E3A;&#xFF1A;</p>
<ol>
<li>&#x5C06;&#x51FD;&#x6570;&#x8BBE;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;</li>
<li>&#x6267;&#x884C;&#x8BE5;&#x51FD;&#x6570;</li>
<li>&#x5220;&#x9664;&#x8BE5;&#x51FD;&#x6570;</li>
</ol>
<p>&#x4EE5;&#x4E0A;&#x4E2A;&#x4F8B;&#x5B50;&#x4E3A;&#x4F8B;&#xFF0C;&#x5C31;&#x662F;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E00;&#x6B65;</span>
foo<span class="token punctuation">.</span>fn <span class="token operator">=</span> bar
<span class="token comment">// &#x7B2C;&#x4E8C;&#x6B65;</span>
foo<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// &#x7B2C;&#x4E09;&#x6B65;</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">.</span>fn
</code></pre>
<p>fn &#x662F;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x540D;&#xFF0C;&#x53CD;&#x6B63;&#x6700;&#x540E;&#x4E5F;&#x8981;&#x5220;&#x9664;&#x5B83;&#xFF0C;&#x6240;&#x4EE5;&#x8D77;&#x6210;&#x4EC0;&#x4E48;&#x90FD;&#x65E0;&#x6240;&#x8C13;&#x3002;</p>
<p>&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x601D;&#x8DEF;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;&#x7740;&#x53BB;&#x5199;&#x7B2C;&#x4E00;&#x7248;&#x7684; <strong>call2</strong> &#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E00;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">call2</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x9996;&#x5148;&#x8981;&#x83B7;&#x53D6;&#x8C03;&#x7528;call&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x7528;this&#x53EF;&#x4EE5;&#x83B7;&#x53D6;</span>
    context<span class="token punctuation">.</span>fn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">delete</span> context<span class="token punctuation">.</span>fn<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

bar<span class="token punctuation">.</span><span class="token function">call2</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<p>&#x6B63;&#x597D;&#x53EF;&#x4EE5;&#x6253;&#x5370; 1 &#x54CE;&#xFF01;&#x662F;&#x4E0D;&#x662F;&#x5F88;&#x5F00;&#x5FC3;&#xFF01;(&#xFF5E;&#xFFE3;&#x25BD;&#xFFE3;)&#xFF5E;</p>
<h2 id="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;"><a name="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;" class="anchor-navigation-ex-anchor" href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x6B65;</h2>
<p>&#x6700;&#x4E00;&#x5F00;&#x59CB;&#x4E5F;&#x8BB2;&#x4E86;&#xFF0C;call &#x51FD;&#x6570;&#x8FD8;&#x80FD;&#x7ED9;&#x5B9A;&#x53C2;&#x6570;&#x6267;&#x884C;&#x51FD;&#x6570;&#x3002;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>foo<span class="token punctuation">,</span> <span class="token string">&apos;kevin&apos;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// kevin</span>
<span class="token comment">// 18</span>
<span class="token comment">// 1</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x5E76;&#x4E0D;&#x786E;&#x5B9A;&#xFF0C;&#x8FD9;&#x53EF;&#x548B;&#x529E;&#xFF1F;</p>
<p>&#x4E0D;&#x6025;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4ECE; Arguments &#x5BF9;&#x8C61;&#x4E2D;&#x53D6;&#x503C;&#xFF0C;&#x53D6;&#x51FA;&#x7B2C;&#x4E8C;&#x4E2A;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x91CC;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x4EE5;&#x4E0A;&#x4E2A;&#x4F8B;&#x5B50;&#x4E3A;&#x4F8B;&#xFF0C;&#x6B64;&#x65F6;&#x7684;arguments&#x4E3A;&#xFF1A;</span>
<span class="token comment">// arguments = {</span>
<span class="token comment">//      0: foo,</span>
<span class="token comment">//      1: &apos;kevin&apos;,</span>
<span class="token comment">//      2: 18,</span>
<span class="token comment">//      length: 3</span>
<span class="token comment">// }</span>
<span class="token comment">// &#x56E0;&#x4E3A;arguments&#x662F;&#x7C7B;&#x6570;&#x7EC4;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x7528;for&#x5FAA;&#x73AF;</span>
<span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> len <span class="token operator">=</span> arguments<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    args<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&apos;arguments[&apos;</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">&apos;]&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6267;&#x884C;&#x540E; args&#x4E3A; [&quot;arguments[1]&quot;, &quot;arguments[2]&quot;, &quot;arguments[3]&quot;]</span>
</code></pre>
<p>&#x4E0D;&#x5B9A;&#x957F;&#x7684;&#x53C2;&#x6570;&#x95EE;&#x9898;&#x89E3;&#x51B3;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x63A5;&#x7740;&#x8981;&#x628A;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x6570;&#x7EC4;&#x653E;&#x5230;&#x8981;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x91CC;&#x9762;&#x53BB;&#x3002;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x5C06;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x5143;&#x7D20;&#x4F5C;&#x4E3A;&#x591A;&#x4E2A;&#x53C2;&#x6570;&#x653E;&#x8FDB;&#x51FD;&#x6570;&#x7684;&#x5F62;&#x53C2;&#x91CC;</span>
context<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span>args<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&apos;,&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// (O_o)??</span>
<span class="token comment">// &#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x80AF;&#x5B9A;&#x662F;&#x4E0D;&#x884C;&#x7684;&#x5566;&#xFF01;&#xFF01;&#xFF01;</span>
</code></pre>
<p>&#x4E5F;&#x8BB8;&#x6709;&#x4EBA;&#x60F3;&#x5230;&#x7528; ES6 &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x8FC7; call &#x662F; ES3 &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x4E3A;&#x4E86;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A; ES3 &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x8981;&#x7528;&#x5230;ES6&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x597D;&#x50CF;&#x2026;&#x2026;&#xFF0C;&#x55EF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5566;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x8FD9;&#x6B21;&#x7528; eval &#x65B9;&#x6CD5;&#x62FC;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">&apos;context.fn(&apos;</span> <span class="token operator">+</span> args <span class="token operator">+</span><span class="token string">&apos;)&apos;</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x8FD9;&#x91CC; args &#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528; Array.toString() &#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x7684;&#x7B2C;&#x4E8C;&#x7248;&#x514B;&#x670D;&#x4E86;&#x4E24;&#x4E2A;&#x5927;&#x95EE;&#x9898;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E8C;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">call2</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    context<span class="token punctuation">.</span>fn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> len <span class="token operator">=</span> arguments<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        args<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&apos;arguments[&apos;</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">&apos;]&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">&apos;context.fn(&apos;</span> <span class="token operator">+</span> args <span class="token operator">+</span><span class="token string">&apos;)&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">delete</span> context<span class="token punctuation">.</span>fn<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

bar<span class="token punctuation">.</span><span class="token function">call2</span><span class="token punctuation">(</span>foo<span class="token punctuation">,</span> <span class="token string">&apos;kevin&apos;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// kevin</span>
<span class="token comment">// 18</span>
<span class="token comment">// 1</span>
</code></pre>
<p>(&#xE51;&#x2022;&#x300;&#x3142;&#x2022;&#x301;)&#x648;&#x2727;</p>
<h2 id="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;"><a name="&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;" class="anchor-navigation-ex-anchor" href="#&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7B2C;&#x4E09;&#x6B65;</h2>
<p>&#x6A21;&#x62DF;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210; 80%&#xFF0C;&#x8FD8;&#x6709;&#x4E24;&#x4E2A;&#x5C0F;&#x70B9;&#x8981;&#x6CE8;&#x610F;&#xFF1A;</p>
<p><strong>1.this &#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x4F20; null&#xFF0C;&#x5F53;&#x4E3A; null &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x89C6;&#x4E3A;&#x6307;&#x5411; window</strong></p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js">
<span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<p>&#x867D;&#x7136;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x672C;&#x8EAB;&#x4E0D;&#x4F7F;&#x7528; call&#xFF0C;&#x7ED3;&#x679C;&#x4F9D;&#x7136;&#x4E00;&#x6837;&#x3002;</p>
<p><strong>2.&#x51FD;&#x6570;&#x662F;&#x53EF;&#x4EE5;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#xFF01;</strong></p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js">
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        value<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">,</span>
        name<span class="token punctuation">:</span> name<span class="token punctuation">,</span>
        age<span class="token punctuation">:</span> age
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">&apos;kevin&apos;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Object {</span>
<span class="token comment">//    value: 1,</span>
<span class="token comment">//    name: &apos;kevin&apos;,</span>
<span class="token comment">//    age: 18</span>
<span class="token comment">// }</span>
</code></pre>
<p>&#x4E0D;&#x8FC7;&#x90FD;&#x5F88;&#x597D;&#x89E3;&#x51B3;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x770B;&#x7B2C;&#x4E09;&#x7248;&#x4E5F;&#x5C31;&#x662F;&#x6700;&#x540E;&#x4E00;&#x7248;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E09;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">call2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> context <span class="token operator">=</span> context <span class="token operator">||</span> window<span class="token punctuation">;</span>
    context<span class="token punctuation">.</span>fn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> len <span class="token operator">=</span> arguments<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        args<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&apos;arguments[&apos;</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">&apos;]&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">&apos;context.fn(&apos;</span> <span class="token operator">+</span> args <span class="token operator">+</span><span class="token string">&apos;)&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">delete</span> context<span class="token punctuation">.</span>fn
    <span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;</span>
<span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        value<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">,</span>
        name<span class="token punctuation">:</span> name<span class="token punctuation">,</span>
        age<span class="token punctuation">:</span> age
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span><span class="token function">call2</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">&apos;kevin&apos;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 1</span>
<span class="token comment">// Object {</span>
<span class="token comment">//    value: 1,</span>
<span class="token comment">//    name: &apos;kevin&apos;,</span>
<span class="token comment">//    age: 18</span>
<span class="token comment">// }</span>
</code></pre>
<p>&#x5230;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x4E86; call &#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#xFF0C;&#x7ED9;&#x81EA;&#x5DF1;&#x4E00;&#x4E2A;&#x8D5E; &#xFF42;&#xFF08;&#xFFE3;&#x25BD;&#xFFE3;&#xFF09;&#xFF44;</p>
<h2 id="apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>apply&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h2>
<p>apply &#x7684;&#x5B9E;&#x73B0;&#x8DDF; call &#x7C7B;&#x4F3C;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x76F4;&#x63A5;&#x7ED9;&#x4EE3;&#x7801;&#xFF0C;&#x4EE3;&#x7801;&#x6765;&#x81EA;&#x4E8E;&#x77E5;&#x4E4E; @&#x90D1;&#x822A;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">apply</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> context <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token operator">||</span> window<span class="token punctuation">;</span>
    context<span class="token punctuation">.</span>fn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> result<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        result <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            args<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&apos;arr[&apos;</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">&apos;]&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        result <span class="token operator">=</span> <span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">&apos;context.fn(&apos;</span> <span class="token operator">+</span> args <span class="token operator">+</span> <span class="token string">&apos;)&apos;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">delete</span> context<span class="token punctuation">.</span>fn
    <span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; YoungGary 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2021-09-09 22:08:17
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="proto.html" class="navigation navigation-prev " aria-label="Previous page: 4.JS原型和原型链">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="ui.html" class="navigation navigation-next " aria-label="Next page: 6.使用Vue cli3搭建组件库并发布到 npm">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5.模拟实现call和apply","level":"1.5.5","depth":2,"next":{"title":"6.使用Vue cli3搭建组件库并发布到 npm","level":"1.5.6","depth":2,"path":"js/ui.md","ref":"js/ui.md","articles":[]},"previous":{"title":"4.JS原型和原型链","level":"1.5.4","depth":2,"path":"js/proto.md","ref":"js/proto.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-comscore","prism","-highlight","copy-code-button","search-pro","-search","-lunr","expandable-chapters","splitter","-sharing","tbfed-pagefooter","anchor-navigation-ex"],"styles":{},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy YoungGary 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"],"lang":{"shell":"bash"}},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"theme-comscore":{},"copy-code-button":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"Gary","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gary的小窝","language":"zh-hans","gitbook":"*","description":"Gary的小窝. 里面包含了个人撰写的技术文章"},"file":{"path":"js/call.md","mtime":"2021-09-09T14:08:17.739Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-09T14:10:10.930Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

